#include "DailySchedule.h"

DailySchedule::DailySchedule(Day day)
{
	this->dayOfWeek = day;
	this->meetings = set<Appointment*,fnCompareAppointment>(Appointment::compare);
	return;
};

DailySchedule::~DailySchedule() 
{
	for (std::set<Appointment*,fnCompareAppointment>::iterator appIter = this->meetings.begin(); 
		appIter!=this->meetings.end(); 
		delete *appIter,++appIter); /*delets all appointments in the day*/  
	return;
};

bool DailySchedule::setAnAppointment(Appointment* app){
	std::pair<std::set<Appointment*,fnCompareAppointment>::iterator,bool> appPair;
	appPair = meetings.insert(app);
	if (appPair.second == false) {
		delete app;
		return false;
	}
	return true;
}


Appointment* DailySchedule::removeAnAppointment(Time const & startTime)
{
	Appointment* app = find(startTime);
	if (app != 0x0){
		meetings.erase(app);
	}
	return app;
}

Appointment* DailySchedule::find(Time const & startTime) const{
	Appointment app = Appointment(startTime,startTime,this->dayOfWeek ,string("N/A"));
	std::set<Appointment*,fnCompareAppointment>::iterator appIter = meetings.find(&app);
	if (appIter == meetings.end() || /*check if there isn't a mach*/
		(*appIter)->getStartTime() != startTime) /*check is the start times mach*/
	{
		return 0x0;
	}
	return *appIter;
}

Appointment* DailySchedule::findAny(Time const & startTime) const{
	Appointment app = Appointment(startTime,startTime,this->dayOfWeek ,string("N/A"));
	std::set<Appointment*,fnCompareAppointment>::iterator appIter = meetings.find(&app);
	if (appIter == meetings.end())/*check if there isn't a mach*/
	{
		return 0x0;
	}
	return *appIter;
}